/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lc3vm;

/**
 *
 * @author Administrator
 */
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;

public class PanelBotones extends JPanel{

   private JButton LoadButton = new JButton("Load");
   //private JButton SaveButton = new JButton("Save");
   private JButton OpenButton = new JButton("Open");
   private JButton RunButton = new JButton("Run");
   private JButton StepButton = new JButton("Step");
   private JButton CompileButton = new JButton("Compile");
   private static String SourceName, LoadFileName, SaveFileName,LoadDirectory, SaveDirectory, OpenFileName, OpenDirectory;
   private static String[] lineaA;
   private int contador = 0;

   public PanelBotones(){

       setLayout(new FlowLayout());

       LoadButton.addActionListener(new ButtonListener());
       //SaveButton.addActionListener(new ButtonListener());
       OpenButton.addActionListener(new ButtonListener());
       RunButton.addActionListener(new ButtonListener());
       StepButton.addActionListener(new ButtonListener());
       CompileButton.addActionListener(new ButtonListener());
       add(LoadButton);
       //add(SaveButton);
       add(OpenButton);
       add(RunButton);
       add(StepButton);
       add(CompileButton);
   }

   public static void loadFile(Frame f, String title, String defDir, String fileType){

       FileDialog fdialog = new FileDialog(f, title, FileDialog.LOAD);
       fdialog.setFile(fileType);
       fdialog.setDirectory(defDir);
       fdialog.setLocation(50, 50);
       fdialog.show();
       LoadFileName = fdialog.getFile();
       LoadDirectory = fdialog.getDirectory();
       SourceName = LoadFileName.substring(0, LoadFileName.length()-4);
    }

   public static void openFile(Frame f, String title, String defDir, String fileType){

       FileDialog fdialog = new FileDialog(f, title, FileDialog.LOAD);
       fdialog.setFile(fileType);
       fdialog.setDirectory(defDir);
       fdialog.setLocation(50, 50);
       fdialog.show();
       OpenFileName = fdialog.getFile();
       OpenDirectory = fdialog.getDirectory();
       SourceName = OpenFileName.substring(0, OpenFileName.length()-4);
    }

   public static void cargarObj() throws FileNotFoundException{
       Metodos.cargar(SourceName + ".obj");
       PanelMemoria.updateMemoria();
   }

   public static void openAsm() throws FileNotFoundException{
       System.out.println(OpenFileName);
       lineaA = Metodos.leer(OpenFileName);
   }

   public static void compilar() throws FileNotFoundException{
        Compilador.reiniciarLinea();
        Metodos.crearSym(SourceName+".sym");

        for(int i = 0; i < lineaA.length; i++) {
            if(lineaA[i].length() > 1)
                Compilador.analizar(lineaA[i]);
            //Registros.setR(1, i);

        }
        Compilador.reiniciarLinea();
        Metodos.crearObj(SourceName + ".obj");
        for(int i = 0; i < lineaA.length; i++) {
            //System.out.println(lineaA[i]);
            if(lineaA[i].length() > 1)
                Compilador.compilar(lineaA[i]);
        }
   }

   public static void ejecutar() throws FileNotFoundException{

        for(int i = 0; i < lineaA.length; i++) {
            System.out.println(lineaA[i]+"Se está ejecutando otra instrucción **********************"+i);
            if(lineaA[i].length() > 1)
                Interpretador.ejecutar(lineaA[i]);
        }

   }


   /*public static void saveFile(Frame f, String title, String defDir, String fileType) {

       FileDialog fdialog = new FileDialog(f, title, FileDialog.SAVE);
       fdialog.setFile(fileType);
       fdialog.setDirectory(defDir);
       fdialog.setLocation(50, 50);
       fdialog.show();
       SaveFileName = fdialog.getFile();
       SaveDirectory = fdialog.getDirectory();
    }*/

   public static String getLoadDirectory(){
       return(LoadDirectory);
   }

   public static String getLoadFileName(){
       return(LoadFileName);
   }

   public static String getSaveDirectory(){
       return(SaveDirectory);
   }

   public static String getSaveFileName(){
       return(SaveFileName);
   }

   private class ButtonListener implements ActionListener{

       public void actionPerformed(ActionEvent event){

            if(event.getSource() == LoadButton){
                try {
                    loadFile(new Frame(), "Load OBJ File", ".\\", "*.obj");
                    cargarObj();

                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            if(event.getSource() == StepButton){
                if(contador>=lineaA.length)
                {
                    System.out.println("Terminó");
                    StepButton.setEnabled(false);
                }
                else
                {
                    try {
                        lineaA = Metodos.leer(SourceName + ".obj");
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    System.out.println(lineaA[contador]+"Se está ejecutando otra instrucción **********************"+contador);
                    if(lineaA[contador].length() > 1)
                        Interpretador.ejecutar(lineaA[contador]);
                    contador++;
                    PanelRegistros.updateRegistros();
                }
            }

            /*if(event.getSource() == SaveButton){
                saveFile(new Frame(), "Open OBJ File", ".\\", "*.obj");
            }*/

            if(event.getSource() == OpenButton){
                try {
                    openFile(new Frame(), "Open TXT File", ".\\", "*.asm");
                    openAsm();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            if (event.getSource() == RunButton){
                try {
                    lineaA = Metodos.leer(SourceName + ".obj");
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                }
                try {
                    ejecutar();
                    PanelRegistros.updateRegistros();
                    //System.out.println(PanelConsola.getPalabra());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                }
                //System.out.println(PanelConsola.getPalabra());

            }
            
            if (event.getSource() == CompileButton){
                try {
                    compilar();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(PanelBotones.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
